package com.rocket.bedroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rocket.bedroom.enums.ResultCode;
import com.rocket.bedroom.pojo.LoginEntity;
import com.rocket.bedroom.pojo.PositionEntity;
import com.rocket.bedroom.response.Result;
import com.rocket.bedroom.service.ILoginService;
import com.rocket.bedroom.service.IPositionService;
import com.rocket.bedroom.utils.SaltUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.swing.text.Position;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 17:23 2020/6/8
 * @ModifyBy:
 */
@Controller
public class LoginController
{


    @Resource
    private ILoginService loginService;

    @Resource
    private IPositionService positionService;


    @PostMapping(value = "/login")
    public ModelAndView userLogin(HttpSession httpSession, String phone, String password)
    {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(phone, password);
        try
        {
            subject.login(token);//用户认证
        } catch (UnknownAccountException e)//用户名不正确
        {
            /*map.put("msg", "用户名不正确");
            return map;*/
            throw new UnknownAccountException("用户名不正确");
        } catch (IncorrectCredentialsException e)//无效的凭证(密码不正确)
        {
            /*map.put("msg", "无效的凭证(密码不正确)");
            return map;*/
            throw new UnknownAccountException("密码不正确");
        }
        LoginEntity loginEntity = (LoginEntity) subject.getPrincipal();
        PositionEntity positionByUser = positionService.getPositionByUser(loginEntity.getPositionId());
        httpSession.setAttribute("operator", loginEntity);
        httpSession.setAttribute("position",positionByUser);
        /*map.put("positionName", positionByUser.getPositionName());
        map.put("userName", loginEntity.getUserName());*/

        //返回当前登录用户信息
        modelAndView.setViewName("register");
        return modelAndView;
    }

    @GetMapping("logout")
    public String logout()
    {
        SecurityUtils.getSubject().logout();
        return "login";
    }

    @PostMapping(value = "/user/add")
    @ResponseBody
    public int addLoginCode(LoginEntity loginEntity)
    {
        Assert.notNull(loginEntity, "add login is null");
        String phone = loginEntity.getPhone();
        Assert.notNull(phone, "add login phone is null");
        Assert.isTrue(null == loginService.findLoginByPhone(phone), "该手机号已注册");
        Assert.notNull(loginEntity.getLoginPassword(), "add login password is null");
        Assert.notNull(loginEntity.getPositionId(), "add login position is null");

        //手机号等于用户登录账号
        loginEntity.setLoginCode(phone);

        //密码加密
        String salt = SaltUtils.getSalt(5);
        Md5Hash md5Hash = new Md5Hash(loginEntity.getLoginPassword(), salt, 1024);
        loginEntity.setLoginPassword(md5Hash.toHex());
        loginEntity.setSalt(salt);

        //如果没有设置用户名
        if (null == loginEntity.getUserName())
        {
            String phoneSub = phone.substring(11 - 4);
            loginEntity.setUserName(phoneSub + "— RockerBedroomUser");
        }
        boolean saveLogin = loginService.save(loginEntity);
        return saveLogin ? 1 : -1;
    }


    @RequestMapping(value = "register")
    public String jumpRegister()
    {
        System.out.println("执行了register");
        return "/register";
    }


}
